package router

import (
	"crow-k8s/src/config"
	"crow-k8s/src/service"
	"github.com/gin-gonic/gin"
)

// CreateConfigmap 创建configmap
// @Summary 创建configmap
// @Description 创建configmap
// @Tags Configmap
// @Accept  application/json
// @Product application/json
// @Param data body service.CreateConfigmapRequest true "创建configmap的信息"
// @Success 200 {object} response.Response{data=v1.ConfigMap} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/configmap [post]
// @Security Bearer
func CreateConfigmap(c *gin.Context) {
	var request service.CreateConfigmapRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	deploymentInfo, err := service.CreateConfigMap(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// GetConfigmapList 查看configmap列表
// @Summary 查看configmap列表
// @Description 查看configmap列表
// @Tags Configmap
// @Param namespace_name query string true "namespace名字"
// @Success 200 {object} response.Response{data=v1.ConfigMapList} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/configmap/list [get]
// @Security Bearer
func GetConfigmapList(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	deploymentList, err := service.GetConfigMapList(config.K8sClientSet, namespaceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentList)
}

// GetConfigmap 查看指定configmap
// @Summary 查看指定configmap
// @Description 查看指定configmap
// @Tags Configmap
// @Param namespace_name query string true "namespace名字"
// @Param name query string true "configmap名字"
// @Success 200 {object} response.Response{data=v1.ConfigMap} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/configmap [get]
// @Security Bearer
func GetConfigmap(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	configmapName := c.Query("name")
	deploymentInfo, err := service.GetConfigMap(config.K8sClientSet, namespaceName, configmapName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// ApplyConfigmap 修改Configmap
// @Summary 修改Configmap
// @Description 修改Configmap
// @Tags Configmap
// @Accept  application/json
// @Product application/json
// @Param data body service.ApplyConfigMapRequest true "创建configmap的信息"
// @Success 200 {object} response.Response{data=v1.ConfigMap} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/configmap [put]
// @Security Bearer
func ApplyConfigmap(c *gin.Context) {
	var request service.ApplyConfigMapRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	deploymentInfo, err := service.ApplyConfigMap(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, deploymentInfo)
}

// DeleteConfigmap 删除指定Configmap
// @Summary 删除指定Configmap
// @Description 删除指定Configmap
// @Tags Configmap
// @Accept  application/json
// @Product application/json
// @Param data body service.DeleteConfigmapRequest true "删除configmap的信息"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/configmap [delete]
// @Security Bearer
func DeleteConfigmap(c *gin.Context) {
	var request service.DeleteConfigmapRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	err = service.DeleteConfigMap(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}
